package S14.second;

import java.util.stream.Stream;

public class F01 {
	public static void main(String[] args) {
		String saying="How old are you?"; //熟能生巧
		String[]ss=saying.split(" "); //获取字符串数组
		System.out.println("各个字符串：");
		//(1)初始值和流中元素具有相同的类型，最终合并为一个字符串结果
		String r = Stream.of(ss).reduce("单词：", (init, word)->init+word+", ");
		System.out.println(r);
		r = Stream.of(ss).reduce("长度：", (init, word)->init+word.length()+", ");
		System.out.println(r);
		//(2)没有初始值，流中第1个元素作为第1个参数充当初始值，第2个元素作为第2个参数
		r=Stream.of(ss).reduce((s1,s2)->{
			System.out.println("s1="+s1+", s2="+s2+", 结果="+s1.toUpperCase()+"*"+s2.toUpperCase());
			return s1.toUpperCase()+"*"+s2.toUpperCase();
		}).get();
		System.out.println(r);
		System.out.println("--------------------");
		Stream.of(ss).reduce((s1,s2)->{
			System.out.println("s1="+s1+", s2="+s2+", 结果="+s1.toUpperCase()+"*"+s2.toUpperCase());
			return s1.toUpperCase()+"*"+s2.toUpperCase();
		}).ifPresent(s->System.out.println(s));
	}
}
